from datetime import date, timedelta
# from django.utils import timezone as date
from django.shortcuts import render
from rest_framework.generics import ListAPIView, ListCreateAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.goods.models import GoodsVisitCount
from apps.meiduo_admin.serializer.uers import GoodsSerializer, UserSerializer, UserCreateSerializer
from apps.meiduo_admin.utils import PageNum
from apps.order.models import OrderInfo
from apps.users.models import User


class UserAllView(APIView):
    # 判断用户是否为管理员
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取今天时间
        today = date.today()
        # 获取数据
        count = User.objects.filter(is_staff=False).count()
        # 返回数据
        return Response({
            'count': count,
            'date': today
        })


class UserTodayView(APIView):
    # 判断用户是否为管理员
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 获取今天时间
        today = date.today()
        # 获取数据
        count = User.objects.filter(date_joined__gte=today, is_staff=False).count()
        count = User.objects.filter(date_joined__gte=today).count()
        # 返回数据
        return Response({
            'count': count,
            'date': today
        })


class UserLasttimeView(APIView):
    # 判断用户是否为管理员
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 查看今日时间
        today = date.today()
        # 查看用户数据表中最后登录日期为今日的用户
        count = User.objects.filter(last_login__gte=today, is_staff=False).count()
        # count = User.objects.filter(last_login__gte=today).count()
        # 返回数据
        return Response({
            'count': count,
            'date': today
        })


class UserOrderView(APIView):
    # 判断用户是否为管理员
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 查看今日时间
        today = date.today()
        # 查看下单表中用户人数
        count = User.objects.filter(orderinfo__create_time__gte=today).count()

        # 返回数据
        return Response({
            'count': count,
            'date': today
        })


class UserMonthView(APIView):
    # 判断用户是否为管理员
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 查看今日时间
        today = date.today()
        # 查勘起始时间
        start = today - timedelta(29)

        list = []
        for i in range(30):
            index = start + timedelta(i)
            next = start + timedelta(i + 1)
            count = User.objects.filter(date_joined__gte=index, date_joined__lt=next).count()
            list.append({
                'count': count,
                'date': index
            })
        # 返回数据
        return Response(list)


class GoodVisitView(APIView):
    # 判断用户是否为管理员
    permission_classes = [IsAdminUser]

    def get(self, request):
        # 查看今日时间
        today = date.today()
        # 查勘起始时间
        goods = GoodsVisitCount.objects.filter(date=today)
        list = []
        for good in goods:
            list.append({
                'count': good.count,
                'category': good.category.name
            })
        # ser = GoodsSerializer(data, mang=True)

        # 返回数据
        return Response(list)


class UsersView(ListCreateAPIView):
    permission_classes = [IsAdminUser]
    pagination_class = PageNum

    def get_serializer_class(self):
        if self.request.method == 'GET':

            return UserSerializer
        else:
            return UserCreateSerializer

    def get_queryset(self):

        keyword = self.request.query_params.get('keyword')

        if keyword is '' or keyword is None:
            return User.objects.filter(is_staff=False).order_by('id')
        else:
            return User.objects.filter(username__contains=keyword).order_by('id')
